﻿using Corina.DataContext.Master;
using Corina.Datamodel.DataObject;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace Corina.Webservice.Cydee
{
    /// <summary>
    /// Summary description for CommonBase
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class CommonBase : WebserviceBase
    {

        #region Industry
        [WebMethod]
        public ResultInfo GetIndustryAllItems(string account, string password)
        {
            return GetInstance<IndustryMaster>().GetAllItems(account, password);
        }

        [WebMethod]
        public ResultInfo InsertIndustry(string account, string password, string title, string description, bool avavailability)
        {
            ResultInfo result = new ResultInfo(ResultValue.Unknown);

            bool exist = GetInstance<MasterBase>().ExistsEntity<IndustryInfo>(DatabaseKeys.CORINA_BASE, new List<string>() { "Title" }, new List<object>() { title });

            if (exist)
            {
                result.Result = ResultValue.Exists;
            }
            else
            {

                IndustryInfo entity = new IndustryInfo()
                {
                    Title = title,
                    Description = description,
                    IsAvailable = avavailability
                };

                result = GetInstance<IndustryMaster>().Update(account, password, entity, EntityOperation.Insert);
            }

            return result;
        }

        [WebMethod]
        public ResultInfo UpdateIndustry(string account, string password, string title, string description, bool avavailability, int id)
        {
            IndustryInfo entity = new IndustryInfo()
            {
                Id = id,
                Title = title,
                Description = description,
                IsAvailable = avavailability
            };

            return GetInstance<IndustryMaster>().Update(account, password, entity, EntityOperation.Update);
        }

        [WebMethod]
        public ResultInfo RemoveIndustry(string account, string password, int id)
        {
            return GetInstance<IndustryMaster>().Update(account, password, new IndustryInfo() { Id = id }, EntityOperation.Delete);
        }

        [WebMethod]
        public ResultInfo ChangeIndustryAvavailability(string account,string password,int id,bool availability)
        {
            return GetInstance<IndustryMaster>().ChangeAvailability(account, password, id, availability);
        }
        #endregion

        #region UserKind
        [WebMethod]
        public ResultInfo GetUserKindAllItems(string account, string password)
        {
            return GetInstance<UserKindMaster>().GetAllItems(account, password);
        }

        [WebMethod]
        public ResultInfo InsertUserKind(string account, string password, string title, string description, bool avavailability)
        {
            ResultInfo result = new ResultInfo(ResultValue.Unknown);

            bool exist = GetInstance<MasterBase>().ExistsEntity<UserKindInfo>(DatabaseKeys.CORINA_BASE, new List<string>() { "Title" }, new List<object>() { title });

            if (exist)
            {
                result.Result = ResultValue.Exists;
            }
            else
            {

                UserKindInfo entity = new UserKindInfo()
                {
                    Title = title,
                    Description = description,
                    IsAvailable = avavailability
                };

                result = GetInstance<UserKindMaster>().Update(account, password, entity, EntityOperation.Insert);
            }

            return result;
        }

        [WebMethod]
        public ResultInfo UpdateUserKind(string account, string password, string title, string description, bool avavailability, int id)
        {
            UserKindInfo entity = new UserKindInfo()
            {
                Id = id,
                Title = title,
                Description = description,
                IsAvailable = avavailability
            };

            return GetInstance<UserKindMaster>().Update(account, password, entity, EntityOperation.Update);
        }

        [WebMethod]
        public ResultInfo RemoveUserKind(string account, string password, int id)
        {
            return GetInstance<UserKindMaster>().Update(account, password, new UserKindInfo() { Id = id }, EntityOperation.Delete);
        }

        [WebMethod]
        public ResultInfo ChangeUserKindAvavailability(string account, string password, int id, bool availability)
        {
            return GetInstance<UserKindMaster>().ChangeAvailability(account, password, id, availability);
        }
        #endregion

        #region UserLevel
        [WebMethod]
        public ResultInfo GetUserLevelAllItems(string account, string password)
        {
            return GetInstance<UserLevelMaster>().GetAllItems(account, password);
        }

        [WebMethod]
        public ResultInfo InsertUserLevel(string account, string password, string title, string description, int level, bool avavailability)
        {
            ResultInfo result = new ResultInfo(ResultValue.Unknown);

            bool exist = GetInstance<MasterBase>().ExistsEntity<UserLevelInfo>(DatabaseKeys.CORINA_BASE, new List<string>() { "Title" }, new List<object>() { title });

            if (exist)
            {
                result.Result = ResultValue.Exists;
            }
            else
            {

                UserLevelInfo entity = new UserLevelInfo()
                {
                    Title = title,
                    Description = description,
                    IsAvailable = avavailability,
                    Level = level
                };

                result = GetInstance<UserLevelMaster>().Update(account, password, entity, EntityOperation.Insert);
            }

            return result;
        }

        [WebMethod]
        public ResultInfo UpdateUserLevel(string account, string password, string title, string description, bool avavailability, int id)
        {
            UserLevelInfo entity = new UserLevelInfo()
            {
                Id = id,
                Title = title,
                Description = description,
                IsAvailable = avavailability
            };

            return GetInstance<UserLevelMaster>().Update(account, password, entity, EntityOperation.Update);
        }

        [WebMethod]
        public ResultInfo RemoveUserLevel(string account, string password, int id)
        {
            return GetInstance<UserLevelMaster>().Update(account, password, new UserLevelInfo() { Id = id }, EntityOperation.Delete);
        }

        [WebMethod]
        public ResultInfo ChangeUserLevelAvavailability(string account, string password, int id, bool availability)
        {
            return GetInstance<UserLevelMaster>().ChangeAvailability(account, password, id, availability);
        }
        #endregion

        #region Guobiao
        [WebMethod]
        public ResultInfo GetAllGuobiaoConfigItems(string account, string password, string name, string code, int specimenKindId, int index, int size)
        {
            return GetInstance<NationalStandardConfigMaster>().GetAllItems(account, password, name, code, specimenKindId, index, size);
        }

        [WebMethod]
        public ResultInfo InsertGuobiaoConfig(string account, string password, int specimenKindId, string name, int documentId, string previewImage)
        {
            return GetInstance<NationalStandardConfigMaster>().Insert(account, password, new Datamodel.Entities.NationalStandardConfigInfo()
            {
                DocumentId = documentId,
                SpecimenKindId = specimenKindId,
                PreviewImage = previewImage,
                Name = name
            });
        }

        /// <summary>
        /// Update national standard configuration.
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <param name="id"></param>
        /// <param name="documentId">The national standard document id.It will not be updated if it's 0.</param>
        /// <param name="previewImage">The name of preview image.It will not be updated if empty.</param>
        /// <returns></returns>
        [WebMethod]
        public ResultInfo UpdateGuobiaoConfig(string account, string password, int id, int documentId, string previewImage)
        {
            return GetInstance<NationalStandardConfigMaster>().Update(account, password, id, documentId, previewImage);
        }

        [WebMethod]
        public ResultInfo RemoveGuobiaoConfig(string account, string password, int id)
        {
            return GetInstance<NationalStandardConfigMaster>().Remove(account, password, id);
        }
        #endregion
    }
}
